home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / vmed.arc / OPTLC3.CCC < prev    next >
Text File  |  1985-12-03  |  3KB  |  153 lines

  1. /* OPTLC/CCC - March 5, 1984 - jwk
  2.     peephole optimizer for LC /ASM files
  3. */
  4. #include stdio/csh
  5. #define YES 1
  6. #define NO 0
  7. #option INLIB
  8. #define HOME 28     /* home cursor */
  9. #define CURDN 0x1A  /* move cursor down one line*/
  10. #define CURRT 0x19  /* move cursor one char to right */
  11. int Inct, Otct;
  12. char *S1, *S2, *S3, *Tcp;
  13.  
  14. main()
  15. {   char *pshhl, *ldhl, *popde, *exde, *gwmac, *clgint;
  16.     int flag;
  17.     fprintf(stderr,"%c%c",28,31);
  18.     fprintf(stderr,"OPTLC - Optimizes LC /ASM files.");
  19.     lcack(1984);
  20.     Inct=Otct=0;
  21.     S1=alloc(256);
  22.     S2=alloc(256);
  23.     S3=alloc(256);
  24.     pshhl="\tPUSH\tHL\n";
  25.     ldhl="\tLD\tHL,";
  26.     gwmac="\t$GETW\t";
  27.     popde="\tPOP\tDE\n";
  28.     exde="\tEX\tDE,HL\n";
  29.     clgint="\tCALL\t@GINT\n";
  30.  
  31.     flag=inplin(S1);
  32.     while (flag) {
  33.         brktst();
  34.         if (ucjr(S1)) {     /* delete unreachable code */
  35.             outlin(S1);
  36.             *S1 = '\0';
  37.             flag=inplin(S1);
  38.             while ((*S1 == '\t') & (*(S1+1) != '$')) {
  39.                 flag=inplin(S1);
  40.                 if (!flag) break;
  41.                 }
  42.         }
  43.         if (strcmp(S1,pshhl)) {     /*not equal*/
  44.             outlin(S1);
  45.             *S1 = '\0';
  46.             flag=inplin(S1);
  47.         }
  48.         else {                      /* was PUSH HL */
  49.             if ((flag=inplin(S2)) &&
  50.                 (strfind(S2,ldhl,0)>=0 ||
  51.                     strfind(S2,gwmac,0)>=0)) {
  52.                 /* was LD HL, */
  53.                 if ((flag=inplin(S3)) &&
  54.                     strcmp(S3,popde)==0) {
  55.                     /* was POP DE */
  56.                     outlin(exde);
  57.                     outlin(S2);
  58.                     *S1 = '\0';
  59.                     *S2 = '\0';
  60.                     flag=inplin(S1);
  61.                 }
  62.                 else {                  /* not POP DE */
  63.                     outlin(S1);
  64.                     outlin(S2);
  65.                     strcpy(S1,S3);
  66.                     *S2 = '\0';
  67.                     *S3 = '\0';
  68.                 }
  69.             }
  70.             else {                  /* not LD HL, */
  71.                 outlin(S1);
  72.                 strcpy(S1,S2);
  73.                 *S2 = '\0';
  74.             }
  75.         }
  76.     }
  77.     outlin(S1);
  78.     exit (0);
  79. }
  80.      
  81. inplin(x)       /* read one line into buffer x */
  82. char *x;
  83. {   int c, flag;
  84.     char *xx;
  85.     flag=TRUE;
  86.     if (x==NULL) x=alloc(256);
  87.     xx=x;
  88.     while ((c=getchar()) != EOF) {
  89.         *x++ = c;
  90.         if (c=='\n') {
  91.             *x = '\0';
  92.             ++Inct;
  93.             report();
  94.             if (*xx == ';')
  95.                 flag=inplin(xx);
  96.             return (flag);
  97.             }
  98.         }
  99.     return (FALSE);
  100. }
  101.  
  102. outlin(x)
  103. char *x;
  104. {   if (x) {
  105.         if (*x) ++Otct;
  106.         while (*x)              /* write to STDOUT */
  107.             putchar(*x++);
  108.         report();
  109.     }
  110. }
  111. brktst()        /* hold up or abort */
  112. {   int c;
  113.     if ((c=inkey())==0) return;
  114.     if (c==1) 0x440d();
  115.     if (c=='`') {
  116.         while (inkey() != ' ')
  117.             ;
  118.     }
  119.     return;
  120. }
  121.  
  122. report()
  123. {   setcur(0,6);
  124.     fprintf(stderr,"Read %6d lines; ",Inct);
  125.     fprintf(stderr,"wrote %6d\n",Otct);
  126. }
  127.  
  128. ucjr(s) char *s;    /* return YES if JP or RET */
  129. {   int temp;
  130.     temp=NO;
  131.     if (strfind(s,"\tRET",0) >= 0)
  132.         temp=YES;
  133.     else if (strfind(s,"\tJP\t",0) >= 0 &&
  134.         strfind(s,",",0) < 0)
  135.             temp=YES;
  136.     return(temp);
  137. }
  138.  
  139. /* set cursor -- 02/11/84 addition.
  140.  * this version should work with any screen size
  141.  * since it always addresses from HOME position!
  142.  */
  143. setcur(x,y)
  144. int x,y;
  145. {   putc(HOME,stderr); /* home the cursor for reference */
  146.     while (y--)
  147.         putc(CURDN,stderr);/* move down to the line */
  148.     while (x--)
  149.     putc(CURRT,stderr);/* then over to the column */
  150. }
  151.  
  152. #include lc/ack
  153.